package org.jimmy.tang.spring.transaction;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author jimmy
 * 
 */
public class BankServiceImpl1 implements BankService1 {
    private UserDao bankDao;
    private TransactionTemplate transactionTemplate;

    /* (non-Javadoc)
     * @see org.jimmy.tang.spring.transaction.BankService1#transfer(long, long, double)
     */
    @Override
    public boolean transfer(final long fromId, final long toId,
            final double amount) {
        return (Boolean) transactionTemplate.execute(new TransactionCallback() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                Object result;
                try {
                    result = bankDao.transfer(fromId, toId, amount);
                } catch (Exception e) {
                    status.setRollbackOnly();
                    result = false;
                    System.out.println("Transfer Error!");
                }
                return result;
            }
        });
    }
}
